package com.zat.mes.andrmes.dofun.system.vm

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.data.BaseResponse
import com.zat.mes.andrmes.data.ClockBean
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import okhttp3.RequestBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Response

/**
 * @author zy
 * @date :2020-02-07 00:05
 * @description: 签到（退）及签到列表
 */
class ClockViewModel(application: Application) : AndroidViewModel(application) {


    private val clockListData: MutableLiveData<List<ClockBean>> by lazy {
        MutableLiveData<List<ClockBean>>().also {

        }
    }
    private val clockDelData: MutableLiveData<String> = MutableLiveData()
    private val clockInOutData: MutableLiveData<String> = MutableLiveData()

    fun getClockList(): LiveData<List<ClockBean>> {
        return clockListData

    }

    fun getClockDel(): LiveData<String> {
        return clockDelData
    }

    fun getClockInOut(): LiveData<String> {
        return clockInOutData
    }


    /**
     * 获取自己的签到记录
     */
    fun loadClockList(day: String, empId: Long) {
        RetrofitManager.run {
            apiService(ApiService::class.java).getClockList(day, empId)
                .enqueue(object : MesCallBack<BaseResponse<List<ClockBean>>>() {
                    override fun onSuccessful(
                        call: Call<BaseResponse<List<ClockBean>>>?,
                        response: Response<BaseResponse<List<ClockBean>>>?
                    ) {
                        var comResultBeanList = response?.body()?.result
                        clockListData.postValue(comResultBeanList)
                    }

                })
        }
    }

    /**
     * 删除
     */
    fun loadDel(workId: Long) {
        RetrofitManager.run {
            apiService(ApiService::class.java).clockDel(workId)
                .enqueue(object : MesCallBack<BaseResponse<String>>() {
                    override fun onSuccessful(
                        call: Call<BaseResponse<String>>?,
                        response: Response<BaseResponse<String>>?
                    ) {
                        if (response?.body()?.success != null) {
                            if (response?.body()?.success!!) {
                                clockDelData.postValue(response?.body()?.message)
                            } else {
                                clockDelData.postValue(response?.body()?.result)
                            }
                        } else {
                            clockDelData.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                        }
                    }


                })
        }
    }

    /**
     * 签到 签退
     */
    fun loadInOut(empId: Long, empName: String, type: String) {
        val jsonObject = JSONObject()
        jsonObject.put("empId", empId)
        jsonObject.put("empName", empName)
        jsonObject.put("type", type)
        val requestBody: RequestBody =
            RequestBody.create(RetrofitManager.JOSNTYPE, jsonObject.toString())
        RetrofitManager.run {
            apiService(ApiService::class.java).clockInOut(requestBody)
                .enqueue(object : MesCallBack<BaseResponse<String>>() {
                    override fun onSuccessful(
                        call: Call<BaseResponse<String>>?,
                        response: Response<BaseResponse<String>>?
                    ) {
                        if (response?.body()?.success != null) {
                            if (response?.body()?.success!!) {
                                clockInOutData.postValue(response?.body()?.message)
                            } else {
                                clockInOutData.postValue(response?.body()?.result)
                            }
                        } else {
                            clockInOutData.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                        }

                    }

                })
        }
    }


}